<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      .banner {
        width: 880px;
        height: 550px;
        position: relative;
        margin: 50px auto;
      }
      ol,
      ul {
        list-style: none;
      }
      #ul li {
        width: 880px;
        height: 550px;
        position: absolute;
        left: 0;
        top: 0;
        display: none;
      }
      #ul li.current {
        display: block;
      }
      .btn {
        height: 80px;
        width: 100%;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
      }
      .btn a {
        float: left;
        height: 80px;
        width: 80px;
        background-color: rgba(255, 255, 255, 0.3);
        margin-left: 10px;
        text-decoration: none;
        color: #444;
        font-size: 60px;
        font-family: 'SimSun';
        text-align: center;
      }
      .btn a:hover {
        background-color: rgba(255, 255, 255, 0.5);
      }
      .btn #rbtn {
        float: right;
        margin-right: 10px;
      }
      #ol {
        width: 200px;
        height: 40px;
        position: absolute;
        bottom: 30px;
        left: 50%;
        transform: translateX(-50%);
        background-color: rgba(255, 255, 255, 0.3);
        border-radius: 20px;
      }
      #ol li {
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background-color: #ccc;
        float: left;
        margin-top: 10px;
        margin-left: 17px;
      }
      #ol li.current {
        background-color: skyblue;
      }
    </style>
  </head>
  <body>
    <div class="banner" id="banner">
      <ul id="ul">
        <li class="current"><img src="./image/lunbo/01.jpg" alt="" /></li>
        <li><img src="./image/lunbo/02.jpg" alt="" /></li>
        <li><img src="./image/lunbo/03.jpg" alt="" /></li>
        <li><img src="./image/lunbo/04.jpg" alt="" /></li>
        <li><img src="./image/lunbo/05.jpg" alt="" /></li>
      </ul>
      <div class="btn">
        <a href="javascript:;" id="lbtn">&lt;</a>
        <a href="javascript:;" id="rbtn">&gt;</a>
      </div>
      <ol id="ol">
        <li class="current"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ol>
    </div>
    <script>
      //1.获取元素
      var banner = my$('banner') //父容器
      var ul = my$('ul') //图片容器
      var ulist = ul.children //所有图片
      var lbtn = my$('lbtn') //左边按钮
      var rbtn = my$('rbtn') //右边按钮
      var ol = my$('ol') //小圆点容器
      var olist = ol.children //所有小圆点
      //2.定义全局控制量
      var index = 0 //控制当前选中的轮播图 默认选中第一张
      var timer //定时器
      //3.左右两边按钮点击切换
      lbtn.onclick = leftHandle
      rbtn.onclick = rightHandle
      //4.点击小圆点切换
      ol.onclick = function (event) {
        // console.log(event)
        if (event.target !== this) {
          index = [...olist].indexOf(event.target)
          change()
        }
      }
      //5.自动轮播
      aotoPlay()
      //6.鼠标事件
      banner.onmouseenter = clearTimer
      banner.onmouseleave = aotoPlay
      //左边按钮处理时间
      function leftHandle() {
        index--
        if (index < 0) {
          index = ulist.length - 1
        }
        change()
      }
      //右边按钮处理事件
      function rightHandle() {
        index++
        if (index >= ulist.length) {
          index = 0
        }
        change()
      }
      //自动轮播
      function aotoPlay() {
        timer = setInterval(rightHandle, 3000)
      }
      //停止轮播
      function clearTimer() {
        clearInterval(timer)
      }
      //切换轮播图
      function change() {
        //清除定时器
        for (var i = 0; i < ulist.length; i++) {
          ulist[i].className = ''
          olist[i].className = ''
        }
        ulist[index].className = 'current'
        olist[index].className = 'current'
        //自动轮播
      }
      //通过id获取元素
      function my$(id) {
        return document.getElementById(id)
      }
    </script>
  </body>
</html>
